* graphrif.do
* Program to graph results of RIF regressions

clear

cap program drop graphrif

program define graphrif

syntax, master(string) ster(string) folder(string) qtiles(numlist) spec(string) time(string) bounds(string) [rareaopts(string) lineopts(string) twowayopts(string) bite(string) freq(string) gen]

	
	cd "`master'/`ster'"

	cd `folder'

	tempvar qtile beta ul ll
	gen `qtile'=.
	gen `beta'=.
	gen `ul'=.
	gen `ll'=.
	
	if "`bite'"=="" loc bite "bite"	
	if "`freq'"=="" loc freq "mensual"

	loc count: word count `qtiles'
	set obs `count'
	loc i=1
	foreach q in `qtiles' {
		qui replace `qtile'=`q' in `i'
		noisily di "1"
		* loc file: dir . files "reg_emp_bp*_p`q'.ster"
		loc file: dir . files "`spec'_`time'_`bite'_*_`freq'_*_`bounds'_p`q'.ster"
		noisily di "2"
		* di "`file'"
		est use `file'
		noisily di "3"
		* di _se[treat]
		qui replace `beta'=_b[treat] in `i'
		noisily di "4"
		if e(bh_wild)!="." qui replace `ul'=real(e(bh_wild)) in `i'
		else replace `ul'=_b[treat]+invt(e(df_r),0.975)*_se[treat] in `i'
		if e(bl_wild)!="." qui replace `ll'=real(e(bl_wild)) in `i'
		else replace `ll'=_b[treat]-invt(e(df_r),0.975)*_se[treat] in `i'
		loc i=`i'+1
		*discard
	}
	
	twoway (rarea `ll' `ul' `qtile', `rareaopts') (line `beta' `qtile', `lineopts') (function y=0, lcolor(gray) range(5 90)), `twowayopts'
	* twoway (rarea `ll' ul q, bcolor(gs13)) (line beta q), legend(label(2 "Coef") label(1 "95 % CI") order(2 1))
	
	if "`gen'"!="" {
		gen qtile=`qtile'
		gen beta=`beta'
		gen ul=`ul'
		gen ll=`ll'
	}
end

	


